package com.stevenmoon.csdnplus.presenter;

import com.stevenmoon.csdnplus.bean.headline.HeadlineColumnBean;
import com.stevenmoon.csdnplus.fragment.HeadlineMainFragment;
import com.stevenmoon.csdnplus.logic.HeadlineManager;
import com.stevenmoon.csdnplus.presenter.base.MvpBaseNoCheckPresenter;

import java.util.List;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WangJun on 2016/4/10.
 */
public class HeadlineMainPresenter extends MvpBaseNoCheckPresenter<HeadlineMainFragment>{


    public void getHeadlineColumns(){
        getView().showLoading();
        Subscription s = HeadlineManager.getHeadlineColumns()
                .subscribeOn(Schedulers.io())
                //TODO 与数据库里保存的栏目比对
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<HeadlineColumnBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showError(e.getMessage());
                    }

                    @Override
                    public void onNext(List<HeadlineColumnBean> headlineColumnBeen) {
                        HeadlineColumnBean top = new HeadlineColumnBean();
                        top.setName("头条");
                        headlineColumnBeen.add(0,top);
                        getView().showHeadlineColumns(headlineColumnBeen);
                    }
                });
        getCompositeSubscription().add(s);
    }

    @Override
    public void detachView(boolean retainInstance) {
        super.detachView(retainInstance);
    }
}
